<?php
class Administrator_Model_News_Model extends Administrator_Model_System{
	public function __construct(){
		$this->db = Zend_Registry::get('vicdbsystem');
		$this->lang = new Nguyen_language_system();
	}
	//////              ajax                /////////////////

	public function updateCatRelated($id,$status){
		$where = "id = '$id'";
		$data = array ('showrelated' => $status );
		$this->db->update('news_cat',$data,$where);
	}
	public function updateCatdate($id,$status){
		$where = "id = '$id'";
		$data = array ('showdate' => $status );
		$this->db->update('news_cat',$data,$where);
	}
	public function updateCatcat($id,$status){
		$where = "id = '$id'";
		$data = array ('showcat' => $status );
		$this->db->update('news_cat',$data,$where);
	}
	public function updateCatsec($id,$status){
		$where = "id = '$id'";
		$data = array ('showsec' => $status );
		$this->db->update('news_cat',$data,$where);
	}
	public function updateCattag($id,$status){
		$where = "id = '$id'";
		$data = array ('showtag' => $status );
		$this->db->update('news_cat',$data,$where);
	}
	public function updateCathit($id,$status){
		$where = "id = '$id'";
		$data = array ('showhit' => $status );
		$this->db->update('news_cat',$data,$where);
	}
	public function updateCatuser($id,$status){
		$where = "id = '$id'";
		$data = array ('showuser' => $status );
		$this->db->update('news_cat',$data,$where);
	}
	public function updateCatsocial($id,$status){
		$where = "id = '$id'";
		$data = array ('showsocial' => $status );
		$this->db->update('news_cat',$data,$where);
	}
	public function updateCatkey($id,$status){
		$where = "id = '$id'";
		$data = array ('key' => $status );
		$this->db->update('news_cat',$data,$where);
	}
	
	
	////////             section           /////////////////////////////////
	public function insertSection($name,$keyword,$description,$status){
		$data = array( 
		'name'  => $name,
		'keyword'  => $keyword,
		'description'  => $description,
		'status'  => $status
		);
		$this->db->insert('news_section',$data);
		return $this->db->lastInsertId();
	}
	public function updateSection($id,$keyword,$descript){
		$where = "id = '$id'";
		$data = array ('keyword' => $keyword,'description' => $descript);
		$this->db->update('news_section',$data,$where);
	}
	////////             catalogue            /////////////////////////////////
	public function insertCatalogue($name,$intro,$image,$keyword,$description,$parent,$section,$status){
		$data = array( 
		'name'  => $name,
		'intro'  => $intro,
		'image'  => $image,
		'keyword'  => $keyword,
		'description'  => $description,
		'parent'  => $parent,
		'section'  => $section,
		'status'  => $status
		);
		$this->db->insert('news_cat',$data);
		return $this->db->lastInsertId();
	}
	public function updateCatalogue($id,$image,$keyword,$description,$parent,$section){
		$where = "id = '$id'";
		$data = array (
		'keyword' => $keyword,
		'image' => $image,
		'parent' => $parent,
		'section' => $section,
		'description' => $description
		);
		$this->db->update('news_cat',$data,$where);
	}
public function updateCatalogueEnable($id,$image,$keyword,$description,$parent,$section,$showrelated,$showdate ,$showsec,$showcat,$showtag,$showhit,$showuser,$showsocial,$key,$view){
		$where = "id = '$id'";
		$data = array (
		'keyword' => $keyword,
		'image' => $image,
		'parent' => $parent,
		'section' => $section,
		'description' => $description,
		'showrelated' => $showrelated,
		'showdate' => $showdate,
		'showcat' => $showcat,
		'showsec' => $showsec,
		'showtag' =>$showtag,
		'showhit' =>$showhit,
		'showuser' => $showuser,
		'showsocial' =>$showsocial,
		'key'=> $key,
		'view'=> $view
		);
		$this->db->update('news_cat',$data,$where);
	}
	public function insertCatalogueEnable($name,$intro,$image,$keyword,$description,$parent,$section,$showrelated,$showdate ,$showsec,$showcat,$showtag,$showhit,$showuser,$showsocial,$key,$view,$status){
		$data = array( 
		'name'  => $name,
		'intro'  => $intro,
		'image'  => $image,
		'keyword'  => $keyword,
		'description'  => $description,
		'parent'  => $parent,
		'section'  => $section,
		'showrelated' => $showrelated,
		'showdate' => $showdate,
		'showcat' => $showcat,
		'showsec' => $showsec,
		'showtag' =>$showtag,
		'showhit' =>$showhit,
		'showuser' => $showuser,
		'showsocial' =>$showsocial,
		'key'=> $key,
		'view' => $view,
		'status'  => $status
		);
		$this->db->insert('news_cat',$data);
		return $this->db->lastInsertId();
	}
	////////             content           /////////////////////////////////
	public function insertContent($name,$intro,$content,$image,$tag,$keyword,$description,$parent,$createdate,$hit,$user,$status){		
		$data = array( 
		'name'  => $name,
		'intro'  => $intro,
		'content'  => $content,
		'image'  => $image,
		'keyword'  => $keyword,
		'description'  => $description,
		'parent'  => $parent,
		'createdate'  => $createdate,
		'hit'  => $hit,
		'user'  => $user,
		'status'  => $status
		);
		$this->db->insert('news_content',$data);
		$id = $this->db->lastInsertId();
		$arrtag = explode(",", $tag); 
		foreach ($arrtag as $value) {
			$tagid = '';
			if($this->checkExistTag($value)==false)$tagid = $this->insertTag($value);
			else $tagid = $this->GetTagIdByName($value);
			$this->insertContentTag($id, $tagid);		
		}
		return $id;
	}
	public function updateContent($id,$image,$tag,$keyword,$description,$parent){
		$where = "id = '$id'";
		$data = array( 
		'image'  => $image,
		'keyword'  => $keyword,
		'description'  => $description,
		'parent'  => $parent
		);
		$this->db->update('news_content',$data,$where);
		$this->deleteContentTagByContent($id);
		$arrtag = explode(",", $tag); 
		foreach ($arrtag as $value) {
			$tagid = '';
			if($this->checkExistTag($value)==false)$tagid = $this->insertTag($value);
			else $tagid = $this->GetTagIdByName($value);
			$this->insertContentTag($id, $tagid);			
		}
	}
	public function insertContentEnable($name,$image,$intro,$content,$tag,$keyword,$description,$parent,$createdate,$hit,$user,$showrelated,$showdate ,$showsec,$showcat,$showtag,$showhit,$showuser,$showsocial,$key,$view,$status){		
		$data = array( 
		'name'  => $name,
		'image'  => $image,
		'intro'  => $intro,
		'content'  => $content,	
		'keyword'  => $keyword,
		'description'  => $description,
		'parent'  => $parent,
		'createdate'  => $createdate,
		'hit'  => $hit,
		'user'  => $user,
		'showrelated' => $showrelated,
		'showdate' => $showdate,
		'showcat' => $showcat,
		'showsec' => $showsec,
		'showtag' =>$showtag,
		'showhit' =>$showhit,
		'showuser' => $showuser,
		'showsocial' =>$showsocial,
		'key'=> $key,
		'view' => $view,
		'status'  => $status
		);
		$this->db->insert('news_content',$data);
		$id = $this->db->lastInsertId();
		$arrtag = explode(",", $tag); 
		foreach ($arrtag as $value) {
			$tagid = '';
			if($this->checkExistTag($value)==false)$tagid = $this->insertTag($value);
			else $tagid = $this->GetTagIdByName($value);
			$this->insertContentTag($id, $tagid);		
		}
		return $id;
	}

	public function updateContentEnanle($id,$image,$tag,$keyword,$description,$parent,$showrelated,$showdate ,$showsec,$showcat,$showtag,$showhit,$showuser,$showsocial,$key,$view){
		$where = "id = '$id'";
		$data = array( 
		'image'  => $image,
		'keyword'  => $keyword,
		'description'  => $description,
		'parent'  => $parent,
		'showrelated' => $showrelated,
		'showdate' => $showdate,
		'showcat' => $showcat,
		'showsec' => $showsec,
		'showtag' =>$showtag,
		'showhit' =>$showhit,
		'showuser' => $showuser,
		'showsocial' =>$showsocial,
		'key'=> $key,
		'view'=> $view
		);
		$this->db->update('news_content',$data,$where);
		$this->deleteContentTagByContent($id);
		$arrtag = explode(",", $tag); 
		foreach ($arrtag as $value) {
			$tagid = '';
			if($this->checkExistTag($value)==false)$tagid = $this->insertTag($value);
			else $tagid = $this->GetTagIdByName($value);
			$this->insertContentTag($id, $tagid);			
		}
	}
	////////             tag           /////////////////////////////////
	public function insertTag($name){
		$data = array('name'  => $name);
		$this->db->insert('news_tag',$data);
		return $this->db->lastInsertId();
	}
	public function GetTagIdByName($name){
		$sql = 'select id from news_tag where name = "'.$name.'"';
		$result = $this->db->fetchRow($sql);
		return $result['id'];
	}
	public function checkExistTag($name){
		$sql = 'select id from news_tag where name = "'.$name.'"';
		$result = $this->db->fetchAll($sql);
		if(count($result)>0) return true;
		return false;
	}
	public function insertContentTag($content,$tag){
		$data = array('content'  => $content,'tag'  => $tag);
		$this->db->insert('news_content_tag',$data);
		return $this->db->lastInsertId();
	}
	public function deleteContentTagByContent($id){
		$where = "content='".$id."'";
		$this->db->delete('news_content_tag',$where);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}